﻿using System;
using Direct.Core;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Direct.Web.Mvc.Controls2;
using Direct.Server.Modules.Shell;
using Direct.ApplicationModel;

namespace Direct.Server.Modules.Shell.Controls
{
    public abstract class ShellApplicationViewBase : UiWidget<ShellApplicationViewBase>
    {
        public static readonly ControlProperty ApplicationIdProperty = ControlProperty.Register("ApplicationId",
            typeof(String),
            typeof(ShellApplicationViewBase));

        public static readonly ControlProperty RootCommandIdProperty = ControlProperty.Register("RootCommandId",
            typeof(String),
            typeof(ShellApplicationViewBase));

        private UiWidgetHeader _title;
        private TableControl _table;
        private TableCell _contentCell;
        private UiSideBar _content;

        public ShellApplicationViewBase()
        {
            this.AddClass("shell-appview");

            _title = new UiWidgetHeader();

            _table = new TableControl();
            _table.NewRow()
                .NewCell()
                .AddClass("shell-content")
                .AddClass("shell-appview-title")
                .Add(_title);

            _content = new UiSideBar();

            _content.LeftBar.OnRenderContent((UiContentContainer left) =>
            {
                var rootCmd = RootCommandId;
                if (rootCmd.IsEmpty()) return;

                //procurar pelo menu
                var app = GetApp();
                if (app == null) return;
                var item = app.Commands.GetItemsForUser(rootCmd, AppUser.Current, 2);
                if (item == null) return;
                //item template
                var itemTemplate = new UiTreeViewItem().SetExpanded(true);
                itemTemplate.ItemTemplate = itemTemplate;
                itemTemplate.LeafIconVisible = true;
                itemTemplate.SetBinding(UiTreeViewItem.ItemsSourceProperty, "Items");

                itemTemplate.Add(new UiLink()
                        .SetBinding(UiLink.IdProperty, "Id", "mi_{0}")
                        .SetBinding(UiLink.HrefProperty, "Url")
                        .SetBinding(UiLink.LinkBehaviorProperty, "Behavior")
                        .SetTextBinding("Caption"));

                var list = new UiList();
                itemTemplate.DataContext = item;
                list.Add(itemTemplate);
                list.Render(CurrentContext);
            });

            _contentCell = _table
                .NewRow()
                .NewCell()
                .AddClass("shell-appview-content")
                .Add(_content);
        }

        public String ApplicationId
        {
            get { return GetValue<String>(ApplicationIdProperty); }
            set { SetValue(ApplicationIdProperty, value); }
        }

        public String RootCommandId
        {
            get { return GetValue<String>(RootCommandIdProperty); }
            set { SetValue(RootCommandIdProperty, value); }
        }

        public UiSideBar Content
        {
            get { return _content; }
        }

        protected override void RenderChildren(IHtmlControlCollection children)
        {
            //base.RenderChildren(children);
        }

        private IApplicationModel GetApp()
        {
            return ServiceLocator.GetInstance<IApplicationModelService>().GetApplication(ApplicationId);
        }

        protected override void RenderContent(string innerText, string rawInnerText)
        {
            var app = GetApp();
            _title.Text = app == null ? "Aplicativo não encontrado: {0}".Templ(ApplicationId) : app.ApplicationTitle;
            _table.Render(CurrentContext);
            base.RenderContent(innerText, rawInnerText);
        }
    }
}
